package com.longfor.dragonshard.controller;

import com.longfor.dragonshard.common.enums.BizEnum;
import com.longfor.dragonshard.dto.*;
import com.longfor.dragonshard.dto.demo.DemoAddDTO;
import com.longfor.dragonshard.dto.demo.DemoAddListDTO;
import com.longfor.dragonshard.dto.demo.DemoDeleteDTO;
import com.longfor.dragonshard.dto.demo.DemoUpdateDTO;
import com.longfor.dragonshard.entity.Demo;
import com.longfor.dragonshard.service.IDemoService;
import com.longfor.drogon.common.bean.LFDatatableExample;
import com.longfor.drogon.common.bean.LFDatatableResultBean;
import com.longfor.drogon.common.bean.LFResultBean;
import com.longfor.drogon.common.dto.SearchListDTO;
import com.longfor.drogon.common.exception.LFBizException;
import com.longfor.drogon.common.exception.LFValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Demo
 *
 * @author chi.zhang
 * @version v1.0
 * @create 2017-10-05 下午2:28
 **/
@CrossOrigin
@RestController
public class DemoController {
    private Logger logger = LoggerFactory.getLogger(DemoController.class);
    @Autowired
    private IDemoService demoService;

    /*
      --------------------------------------------------------
      -- admin接口
      --------------------------------------------------------
     */

    /**
     * 新增
     *
     * @param demoAddDTO
     * @param errors
     * @return
     */
    @PostMapping(value = "/admin/api/demo/add")
    public LFResultBean add(@Valid @RequestBody DemoAddDTO demoAddDTO, Errors errors) {
        //服务端数据校验
        if (errors.hasErrors()) {
            throw new LFValidationException(errors);
        }

        //数据转换
        Demo demo = new Demo();
        ConvertMapper.INSTANCE.updateEntityFromDTO(demoAddDTO, demo);

        return new LFResultBean<>(demoService.insert(demo));
    }

    /**
     * 批量新增
     * 对于DemoAddListDTO的校验：
     *     List中的对象依然会基于JSR303进行校验
     *     List属性要增加@Valid注解，否则无效
     *
     * @param demoAddListDTO
     * @param errors
     * @return
     */
    @PostMapping(value = "/admin/api/demo/addList")
    public LFResultBean addList(@Valid @RequestBody DemoAddListDTO demoAddListDTO, Errors errors) {
        //服务端数据校验
        if (errors.hasErrors()) {
            throw new LFValidationException(errors);
        }

        //数据转换
        List<Demo> demoList = new ArrayList<>();
        ConvertMapper.INSTANCE.updateEntityFromDTO(demoAddListDTO.getList(), demoList);

        return new LFResultBean<>(demoService.insertList(demoList));
    }

    /**
     * 修改
     *
     * @param demoUpdateDTO
     * @param errors
     * @return
     */
    @PostMapping(value = "/admin/api/demo/update")
    public LFResultBean update(@Valid @RequestBody DemoUpdateDTO demoUpdateDTO, Errors errors) {
        //服务端数据校验
        if (errors.hasErrors()) {
            throw new LFValidationException(errors);
        }

        //数据转换
        Demo demo = new Demo();
        ConvertMapper.INSTANCE.updateEntityFromDTO(demoUpdateDTO, demo);

        demoService.update(demo);
        return new LFResultBean();
    }

    /**
     * 删除
     *
     * @param demoDeleteDTO
     * @return
     */
    @PostMapping(value = "/admin/api/demo/delete")
    public LFResultBean delete(@Valid @RequestBody DemoDeleteDTO demoDeleteDTO, Errors errors) {
        if (errors.hasErrors()) {
            throw new LFValidationException(errors);
        }

        return new LFResultBean<>(demoService.delete(demoDeleteDTO.getId()));
    }

    /*
      --------------------------------------------------------
      -- web接口
      --------------------------------------------------------
     */

    /**
     * 查询
     *
     * @param searchListDTO
     * @param errors
     * @return
     * @throws UnsupportedEncodingException
     */
    @PostMapping(value = "/web/api/demo/searchList")
    public LFDatatableResultBean searchList(@Valid @RequestBody SearchListDTO searchListDTO, Errors errors)
            throws IllegalAccessException {
        if (errors.hasErrors()) {
            throw new LFValidationException(errors);
        }
        LFDatatableExample lfDatatableExample = new LFDatatableExample(searchListDTO, Demo.class);
        lfDatatableExample.setSearchColumns("name", "remark");
        lfDatatableExample.setOrderByClause("create_time DESC");
        return demoService.searchByExample(lfDatatableExample);
    }

    /**
     * 查询多个id，每个id之间用英文逗号分隔
     * 注意：此方法不会使用缓存
     *
     * @param ids e.g. 1,2,3
     * @return
     * @throws IllegalAccessException
     */
    @GetMapping(value = "/web/api/demo/selectByIds/{ids}")
    public LFResultBean selectByIds(@PathVariable(value = "ids") String ids)
            throws IllegalAccessException {
        return new LFResultBean<>(demoService.selectByIds(ids));
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return LFResultBean
     */
    @GetMapping(value = "/web/api/demo/get/{id}")
    public LFResultBean<Demo> get(@PathVariable(value = "id") Long id) {
        return new LFResultBean<>(demoService.selectById(id).orElseThrow(() -> new LFBizException(BizEnum.E9901.getText())));
    }

}
